package com.tjdx.projectsubmission.server;

import smile.*;

public class CollapseTest {
    public static void run() {
        System.out.println("Starting CollapseTest...");
        Network net = new Network();


        // 创建节点
        int rockty = createCptNode(net, "Rockty", "Rock type", new String[]{"M1", "M2", "M3"}, 100, 100);
        int geocon = createCptNode(net, "Geocon", "Geo conditions", new String[]{"M1", "M2", "M3"}, 200, 100);
        int undwater = createCptNode(net, "Undwater", "Under water", new String[]{"M1", "M2", "M3"}, 300, 100);
        int depburial = createCptNode(net, "Depburial", "Depth of burial", new String[]{"M1", "M2"}, 100, 200);
        int span = createCptNode(net, "Span", "Span length", new String[]{"M1", "M2"}, 200, 200);
        int totalgeocon = createCptNode(net, "Totalgeocon", "Total geo conditions", new String[]{"M1", "M2"}, 300, 200);
        int distbadj = createCptNode(net, "Distbadj", "Distance to bad junction", new String[]{"M1", "M2", "M3"}, 100, 300);
        int distbbla = createCptNode(net, "Distbbla", "Distance to bad blockage", new String[]{"M1", "M2", "M3"}, 200, 300);
        int stancon = createCptNode(net, "Stancon", "Standard conditions", new String[]{"M1", "M2", "M3"}, 300, 300);
        int maqual = createCptNode(net, "Maqual", "Material quality", new String[]{"M1", "M2", "M3"}, 100, 400);
        int designerr = createCptNode(net, "Designerr", "Design error", new String[]{"M1", "M2"}, 200, 400);
        int intime = createCptNode(net, "Intime", "Installation time", new String[]{"M1", "M2"}, 300, 400);
        int conqual = createCptNode(net, "Conqual", "Construction quality", new String[]{"M1", "M2"}, 100, 500);
        int stability = createCptNode(net, "Stability", "Structural stability", new String[]{"M1", "M2"}, 200, 500);
        int totaldistb = createCptNode(net, "Totaldistb", "Total disturbances", new String[]{"M1", "M2"}, 300, 500);
        int totalstre = createCptNode(net, "Totalstre", "Total stress", new String[]{"M1", "M2"}, 100, 600);
        int concollapse = createCptNode(net, "Concollapse", "Construction collapse", new String[]{"M1", "M2"}, 200, 600);

        // 创建节点连接
        net.addArc(rockty, totalgeocon);
        net.addArc(geocon, totalgeocon);
        net.addArc(undwater, totalgeocon);

        net.addArc(depburial, stability);
        net.addArc(span, stability);
        net.addArc(totalgeocon, stability);

        net.addArc(distbadj, totaldistb);
        net.addArc(distbbla, totaldistb);

        net.addArc(stancon, conqual);
        net.addArc(maqual, conqual);

        net.addArc(designerr, totalstre);
        net.addArc(intime, totalstre);
        net.addArc(conqual, totalstre);

        net.addArc(conqual, concollapse);
        net.addArc(stability, concollapse);
        net.addArc(totalgeocon, concollapse);
        net.addArc(totaldistb, concollapse);
        net.addArc(totalstre, concollapse);

        double[] rocktyDef = {
                0.33	, //	P(Rockty=M1)
                0.335 , //	P(Rockty=M2)
                0.335 //	P(Rockty=M3)
        };
        net.setNodeDefinition(rockty, rocktyDef);

        double[] geoconDef = {
                0.62	, //	P(Geocon=M1)
                0.28	, //	P(Geocon=M2)
                0.1	 //	P(Geocon=M3)
        };
        net.setNodeDefinition(geocon, geoconDef);


        double[] undwaterDef = {
                0.7	, //	P(Undwater=M1)
                0.2	, //	P(Undwater=M2)
                0.1	 //	P(Undwater=M3)
        };
        net.setNodeDefinition(undwater, undwaterDef);

        double[] depburialDef = {
                0.8	, //	P(Depburial=M1)
                0.2	//	P(Depburial=M2)
        };
        net.setNodeDefinition(depburial, depburialDef);


        double[] spanDef = {
                0.9	, //	P(Span=M1)
                0.1	//	P(Span=M2)
        };
        net.setNodeDefinition(span, spanDef);

        double[] distbadjDef = {
                0.8	, //	P(Distbadj=M1)
                0.15	, //	P(Distbadj=M2)
                0.05	 //	P(Distbadj=M3)
        };
        net.setNodeDefinition(distbadj, distbadjDef);

        double[] distbblaDef = {
                0.7	, //	P(Distbbla=M1)
                0.2	, //	P(Distbbla=M2)
                0.1	 //	P(Distbbla=M3)
        };
        net.setNodeDefinition(distbbla, distbblaDef);

        double[] stanconDef = new double[] {
                0.95,  // P(Stancon=M1)
                0.03,  // P(Stancon=M2)
                0.02   // P(Stancon=M3)
        };
        net.setNodeDefinition("Stancon", stanconDef);

        double[] maqualDef = new double[] {
                0.88	, //	P(Maqual=M1)
                0.08	, //	P(Maqual=M2)
                0.04	 //	P(Maqual=M3)
        };
        net.setNodeDefinition("Maqual", maqualDef);

        double[] designerrDef = new double[] {
                0.99, // P(Designerr=M1)
                0.01  // P(Designerr=M2)
        };
        net.setNodeDefinition("Designerr", designerrDef);

        double[] intimeDef = new double[] {
                0.9,  // P(Intime=M1)
                0.1   // P(Intime=M2)
        };
        net.setNodeDefinition("Intime", intimeDef);
        double[] conqualDef = new double[] {
                0.001, 0.999	, //	P(Maqual=M1|Stancon=M1)
                0.019, 0.981	, //	P(Maqual=M2|Stancon=M1)
                0.129, 0.871	, //	P(Maqual=M3|Stancon=M1)

                0.015,0.985	, //	P(Maqual=M1|Stancon=M2)
                0.135, 0.865	, //	P(Maqual=M2|Stancon=M2)
                0.249, 0.851	, //	P(Maqual=M3|Stancon=Mw)

                0.04	, 0.96,//	P(Maqual=M1|Stancon=M3)
                0.25	, 0.75,//	P(Maqual=M2|Stancon=M3)
                0.69, 0.31	//	P(Maqual=M3|Stancon=M3)
        };
        net.setNodeDefinition(conqual, conqualDef);

        double[] stabilityDef = new double[] {
                0.001, 0.999	, //	P(Totalgeocon=M1|Span=M1, Depburial=M1)
                0.999, 0.001	, //	P(Totalgeocon=M2|Span=M1, Depburial=M1)

                0.01	, 0.99, //	P(Totalgeocon=M1|Span=M1, Depburial=M2)
                0.99	, 0.01, //	P(Totalgeocon=M2|Span=M1, Depburial=M2)

                0.13	, 0.97,//	P(Totalgeocon=M1|Span=M2, Depburial=M1)
                0.97	, 0.13,//	P(Totalgeocon=M2|Span=M2, Depburial=M1)

                0.36	, 0.64, //	P(Totalgeocon=M1|Span=M2, Depburial=M2)
                0.64 , 0.63	//	P(Totalgeocon=M2|Span=M2, Depburial=M2)
        };
        net.setNodeDefinition("Stability", stabilityDef);



        double[] totalgeoconDef = new double[] {
                0.001, 0.999	, //	P(Geocon=M1|Rockty=M1, Undwater=M1)
                0.01, 0.99	,  //	P(Geocon=M2|Rockty=M1, Undwater=M1)
                0.989, 0.11	, //	P(Geocon=M3|Rockty=M1, Undwater=M1)

                0.05, 0.95	, //	P(Geocon=M1|Rockty=M2, Undwater=M1)
                0.1	, 0.9, //	P(Geocon=M2|Rockty=M2, Undwater=M1)
                0.85, 0.15	, //	P(Geocon=M3|Rockty=M2, Undwater=M1)

                0.02	, 0.98,//	P(Geocon=M1|Rockty=M3, Undwater=M1)
                0.09	, 0.91 , //	P(Geocon=M2|Rockty=M3, Undwater=M1)
                0.15	, 0.85,//	P(Geocon=M3|Rockty=M3, Undwater=M1)

                0.09	, 0.91,//	P(Geocon=M1|Rockty=M1, Undwater=M2)
                0.11	, 0.89,//	P(Geocon=M2|Rockty=M1, Undwater=M2)
                0.2	, 0.8,//	P(Geocon=M3|Rockty=M1, Undwater=M2)

                0.02	, 0.98,//	P(Geocon=M1|Rockty=M2, Undwater=M2)
                0.13	, 0.87,//	P(Geocon=M2|Rockty=M2, Undwater=M2)
                0.25	, 0.75,//	P(Geocon=M3|Rockty=M2, Undwater=M2)

                0.04	, 0.96, //	P(Geocon=M1|Rockty=M3, Undwater=M2)
                0.15	, 0.85,//	P(Geocon=M2|Rockty=M3, Undwater=M2)
                0.21	, 0.79,//	P(Geocon=M3|Rockty=M3, Undwater=M2)

                0.01	, 0.99,//	P(Geocon=M1|Rockty=M1, Undwater=M3)
                0.12	, 0.88,//	P(Geocon=M2|Rockty=M1, Undwater=M3)
                0.17	, 0.93,//	P(Geocon=M3|Rockty=M1, Undwater=M3)

                0.02	, 0.98,//	P(Geocon=M1|Rockty=M2, Undwater=M3)
                0.13	, 0.87,//	P(Geocon=M2|Rockty=M2, Undwater=M3)
                0.25	, 0.75,//	P(Geocon=M3|Rockty=M2, Undwater=M3)

                0.06	, 0.94,//	P(Geocon=M1|Rockty=M3, Undwater=M3)
                0.13	, 0.87,//	P(Geocon=M2|Rockty=M3, Undwater=M3)
                0.21, 0.79	//	P(Geocon=M3|Rockty=M3, Undwater=M3)
        };
        net.setNodeDefinition("Totalgeocon", totalgeoconDef);

        double[] totaldistbDef = new double[] {
                0.0001	, 0.9999,//	P(Distbadj=M1|Distbbla=M1)
                0.012	, 0.988,//	P(Distbadj=M1|Distbbla=M2)
                0.1	, 0.9,//	P(Distbadj=M1|Distbbla=M3)

                0.002	, 0.998,//	P(Distbadj=M2|Distbbla=M1)
                0.05	, 0.95,//	P(Distbadj=M2|Distbbla=M2)
                0.13	, 0.87,//	P(Distbadj=M2|Distbbla=M3)

                0.06	, 0.94,//	P(Distbadj=M3|Distbbla=M1)
                0.15, 0.85	, //	P(Distbadj=M3|Distbbla=M2)
                0.25, 0.75	 //	P(Distbadj=M3|Distbbla=M3)
        };
        net.setNodeDefinition("Totaldistb", totaldistbDef);

        double[] totalstrebDef = new double[] {
                0.0001	, 0.9999,//	P(Intime=M1|Designerr=M1, Conqual=M1)
                0.9999	, 0.0001,//	P(Intime=M2|Designerr=M1, Conqual=M1)

                0.12	, 0.88,//	P(Intime=M1|Designerr=M2, Conqual=M1)
                0.88	, 0.12,//	P(Intime=M2|Designerr=M2, Conqual=M1)

                0.26	, 0.74,//	P(Intime=M1|Designerr=M1, Conqual=M2)
                0.74	, 0.26,//	P(Intime=M2|Designerr=M1, Conqual=M2)

                0.39, 0.61	, //	P(Intime=M1|Designerr=M2, Conqual=M2)
                0.61, 0.39	 //	P(Intime=M2|Designerr=M2, Conqual=M2)

        };
        net.setNodeDefinition("Totalstre", totalstrebDef);


        double[] concollapsebDef = new double[] {
                0.00001	,	0.99999	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M1, Stability=M1, Totaldistb=M1)
                0.99999	,	0.00001	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M1, Stability=M1, Totaldistb=M1)

                0.0001	,	0.9999	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M1, Stability=M1, Totaldistb=M1)
                0.9999	,	0.0001	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M1, Stability=M1, Totaldistb=M1)

                0.0012	,	0.9988	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M2, Stability=M1, Totaldistb=M1)
                0.9988	,	0.0012	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M2, Stability=M1, Totaldistb=M1)

                0.0025	,	0.9975	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M2, Stability=M1, Totaldistb=M1)
                0.9975	,	0.0025	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M2, Stability=M1, Totaldistb=M1)

                0.01	,	0.99	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M1, Stability=M2, Totaldistb=M1)
                0.99	,	0.01	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M1, Stability=M2, Totaldistb=M1)

                0.025	,	0.975	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M1, Stability=M2, Totaldistb=M1)
                0.975	,	0.025	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M1, Stability=M2, Totaldistb=M1)

                0.014	,	0.986	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M2, Stability=M2, Totaldistb=M1)
                0.986	,	0.014	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M2, Stability=M2, Totaldistb=M1)

                0.055	,	0.945	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M2, Stability=M2, Totaldistb=M1)
                0.945	,	0.055	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M2, Stability=M2, Totaldistb=M1)

                0.02	,	0.98	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M1, Stability=M1, Totaldistb=M2)
                0.98	,	0.02	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M1, Stability=M1, Totaldistb=M2)

                0.025	,	0.975	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M1, Stability=M1, Totaldistb=M2)
                0.975	,	0.025	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M1, Stability=M1, Totaldistb=M2)

                0.035	,	0.965	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M2, Stability=M1, Totaldistb=M2)
                0.965	,	0.035	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M2, Stability=M1, Totaldistb=M2)

                0.045	,	0.955	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M2, Stability=M1, Totaldistb=M2)
                0.955	,	0.045	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M2, Stability=M1, Totaldistb=M1)

                0.03	,	0.97	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M1, Stability=M2, Totaldistb=M2)
                0.97	,	0.03	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M1, Stability=M2, Totaldistb=M2)

                0.04	,	0.96	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M1, Stability=M2, Totaldistb=M2)
                0.96	,	0.04	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M1, Stability=M2, Totaldistb=M2)

                0.06	,	0.94	,	//	P(Conqual=M1|Totalstre=M1, Totalgeocon=M2, Stability=M2, Totaldistb=M2)
                0.94	,	0.06	,	//	P(Conqual=M2|Totalstre=M1, Totalgeocon=M2, Stability=M2, Totaldistb=M2)

                0.09	,	0.91	,	//	P(Conqual=M1|Totalstre=M2, Totalgeocon=M2, Stability=M2, Totaldistb=M2)
                0.91	,	0.09	,	//	P(Conqual=M2|Totalstre=M2, Totalgeocon=M2, Stability=M2, Totaldistb=M2)
        };
        net.setNodeDefinition(concollapse, concollapsebDef);

        // 写入XDSL文件
        net.writeFile("ComplexBayesianNetwork.xdsl");

        System.out.println("ComplexBayesianNetwork complete: Network written to ComplexBayesianNetwork.xdsl");
    }

    private static int createCptNode(Network net, String id, String name, String[] outcomes, int xPos, int yPos) {
        int handle = net.addNode(Network.NodeType.CPT, id);
        net.setNodeName(handle, name);
        net.setNodePosition(handle, xPos, yPos, 85, 55);
        int initialOutcomeCount = net.getOutcomeCount(handle);
        for (int i = 0; i < initialOutcomeCount; i ++) {
            net.setOutcomeId(handle, i, outcomes[i]);
        }

        for (int i = initialOutcomeCount; i < outcomes.length; i ++) {
            net.addOutcome(handle, outcomes[i]);
        }
        return handle;
    }
}
